package com.aiwei.tdjk.service.impl;

import com.aiwei.tdjk.dao.mbg.UserOrganizationJobMapper;
import com.aiwei.tdjk.entity.UserOrganizationJob;
import com.aiwei.tdjk.entity.UserOrganizationJobExample;
import com.aiwei.tdjk.service.UserOrganizationJobService;
import com.aiwei.tdjk.service.UserService;
import com.aiwei.tdjk.utils.UserUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by yanfeng on 2016/12/13.
 */
@Service
public class UserOrganizationJobServiceImpl implements UserOrganizationJobService {

    @Autowired
    private UserOrganizationJobMapper userOrganizationJobMapper;
    @Autowired
    private UserService userService;

    @Override
    public List<UserOrganizationJob> getByUserId(long userId) {
        UserOrganizationJobExample example = new UserOrganizationJobExample();
        example.or().andUserIdEqualTo(userId);
        return userOrganizationJobMapper.selectByExample(example);
    }

    /**
     * 新增
     *
     * @param userOrganizationJob
     */
    @Override
    public void add(UserOrganizationJob userOrganizationJob) {
        userOrganizationJobMapper.insertSelective(userOrganizationJob);
    }

    /**
     * 根据用户的id删除
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(Long userId) {
        UserOrganizationJobExample example = new UserOrganizationJobExample();
        example.or().andUserIdEqualTo(userId);
        userOrganizationJobMapper.deleteByExample(example);
    }

    @Override
    public List<Long> findUserIdsByJobIds(List<Long> jobIds) {
        Set<Long> set = Sets.newHashSet();//用于去除重复的userId
        UserOrganizationJobExample example = new UserOrganizationJobExample();
        example.or().andJobIdIn(jobIds);
        List<UserOrganizationJob> uoj = userOrganizationJobMapper.selectByExample(example);
        if (null != uoj && uoj.size() > 0) {
            //获得userIds
            for (UserOrganizationJob item : uoj) {
                set.add(item.getUserId());
            }
        }
        List<Long> list = new ArrayList<>(set);//set转list
        return list;
    }

    @Override
    public Long findDepartmentIdByLoginIngUser() {
        //获得当前登陆用户id
        Long departmentId=null;
        Long userId = UserUtil.getUserFromSession().getId();
        List<Long> list = Lists.newArrayList();
        UserOrganizationJobExample example = new UserOrganizationJobExample();
        example.or().andUserIdEqualTo(userId);
        List<UserOrganizationJob> uoj = userOrganizationJobMapper.selectByExample(example);
        if (null != uoj && uoj.size() > 0) {
            //获得jobIds
            UserOrganizationJob userOrganizationJob = uoj.get(0);
            departmentId = userOrganizationJob.getOrganizationId();
        }
        return departmentId;
    }

    @Override
    public List<Long> getUserIdsOfSameOrganizationByUserId(Long userId) {
        List<Long> organizationIds = Lists.newArrayList();//组织机构ids
        List<UserOrganizationJob> userOrganizationJobs = getByUserId(userId);//用户--组织--职务对象
        for(UserOrganizationJob userOrganizationJob:userOrganizationJobs){
            organizationIds.add(userOrganizationJob.getOrganizationId());
        }
        //查找包含组织机构organizationIds的所有用户ids
        List<Long> userIds = Lists.newArrayList();//用户ids
        UserOrganizationJobExample example = new UserOrganizationJobExample();
        if(organizationIds!=null && organizationIds.size()>0){
            example.or().andOrganizationIdIn(organizationIds);
            List<UserOrganizationJob> userOrganizationJobs1 = userOrganizationJobMapper.selectByExample(example);
            for(UserOrganizationJob userOrganizationJob1:userOrganizationJobs1){
                Boolean flag = userService.isOnJob(userOrganizationJob1.getUserId());
                if(flag){
                    userIds.add(userOrganizationJob1.getUserId());
                }
            }
        }
        return userIds;
    }
}
